package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.util.Version;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.StockSku;
@Repository
public class SearchDaoImpl {
	
	@Autowired
	private SessionFactory sessionFactory;
	@SuppressWarnings({ "unchecked", "deprecation" })
	public void index() {
		Session session =  sessionFactory.getCurrentSession();
		
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		//Transaction tx = fullTextSession.beginTransaction();
		List<Lead> leads = session.createQuery("from Lead as lead").list();
		for (Lead lead : leads) {
		fullTextSession.index(lead);
		}
		List<Account> accounts = session.createQuery("from Account as account").list();
		for (Account account : accounts) {
		fullTextSession.index(account);
		}
		List<Contact> contacts= session.createQuery("from Contact as contact").list();
		for (Contact contact : contacts) {
		fullTextSession.index(contact);
		}
		List<StockSku> skus = session.createQuery("from StockSku as sku").list();
		for (StockSku sku : skus) {
		fullTextSession.index(sku);
		}
		
		fullTextSession.flushToIndexes();
	}
	
	@SuppressWarnings("unchecked")
	public Set<Lead> searchLead(String query) {
		
		Session session = sessionFactory.getCurrentSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		//String [] fields = new String[]{"fName","lName","companyName","title","phone","email","webSite","description","zipCode","state","street","state"};
		String [] fields = new String[]{"fName","lName","companyName"};
		MultiFieldQueryParser multiFieldQueryParser =new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, new SimpleAnalyzer());
		try {
			//multiFieldQueryParser.setAllowLeadingWildcard(true);
			query = query+"*";
			org.apache.lucene.search.Query query2 = multiFieldQueryParser.parse(query);
			org.hibernate.Query query3 = fullTextSession.createFullTextQuery(query2, Lead.class);
			List<Lead> leads = new ArrayList<Lead>();
			leads = query3.list();
			Set<Lead>leads2 = new HashSet<Lead>();
			for (Lead lead : leads) {
				leads2.add(lead);
			}
			return leads2;
		} catch (ParseException e) {
			e.printStackTrace();
			return new HashSet<Lead>();
		}
	}
	
	@SuppressWarnings("unchecked")
	public Set<Account> searchAccount(String query) {
		
		Session session = sessionFactory.getCurrentSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		//String [] fields = new String[]{"phone","accountName","email","webSite","description","zipCode","state","street","fax","billingAdress","shipingAddress"};
		String [] fields = new String[]{"accountName","email","companyName"};
		MultiFieldQueryParser multiFieldQueryParser =new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, new SimpleAnalyzer());
		try {
			query = query+"*";
			org.apache.lucene.search.Query query2 = multiFieldQueryParser.parse(query);
			org.hibernate.Query query3 = fullTextSession.createFullTextQuery(query2, Account.class);
			List<Account> accounts = new ArrayList<Account>();
			 accounts = query3.list();
			 Set<Account>accounts2 = new HashSet<Account>();
				for (Account account : accounts) {
					accounts2.add(account);
				}
			return  accounts2;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Contact> searchContact(String query) {
		
		Session session = sessionFactory.getCurrentSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		//String [] fields = new String[]{"phone","email","webSite","parentName","mobile","lName","fName"};
		String [] fields = new String[]{"email","lName","fName"};
		MultiFieldQueryParser multiFieldQueryParser =new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, new SimpleAnalyzer());
		try {
			query = query+"*";
			org.apache.lucene.search.Query query2 = multiFieldQueryParser.parse(query);
			org.hibernate.Query query3 = fullTextSession.createFullTextQuery(query2, Contact.class);
			List<Contact> contacts = new ArrayList<Contact>();
			 contacts = query3.list();
			return  contacts;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public Set<StockSku> searchSku(String query) {
		Session session = sessionFactory.getCurrentSession();
//		FullTextSession fullTextSession = Search.getFullTextSession(session);
//		String [] fields = new String[]{"name","code"};
//		MultiFieldQueryParser multiFieldQueryParser =new MultiFieldQueryParser(fields, new StandardAnalyzer());
//		try {
//			query = query+"*";
//			org.apache.lucene.search.Query query2 = multiFieldQueryParser.parse(query);
//			org.hibernate.Query query3 = fullTextSession.createFullTextQuery(query2, StockSku.class);
//			List<StockSku> skus = new ArrayList<StockSku>();
//			 skus = query3.list();
//			 Set<StockSku> skus2 = new HashSet<StockSku>();
//				for (StockSku sku : skus) {
//					skus2.add(sku);
//				}
//			return  skus2;
//		} catch (ParseException e) {
//			e.printStackTrace();
//			return null;
//		}
		Query query2 = session.createQuery("from StockSku where code= :query or lower(name) like :query");
		query = query.toLowerCase();
		String queries = "%"+query+"%";
		query2.setParameter("query", queries);
		List<StockSku> list =  query2.list();
		Set<StockSku> skus2 = new HashSet<StockSku>();
		for (StockSku sku : list) {
			skus2.add(sku);
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+skus2.size());
		return  skus2;
	}
}
